<?php

require_once('lib/Domain.class.php');
require_once('lib/Message.class.php');


/**
 * domains()
 *
 * Returns all available domains
 *
 */
function domains() {
	$domains = Domain::domains();

	$results = array();

	foreach($domains as $domain) {
		$tempArray = array('id' => $domain->getID(),
					'domain'=>$domain->domain,
					'local'=>$domain->local
				);
		array_push($results, $tempArray);

	}


	return $results;

}


/**
 * domain()
 *
 * Returns domain with the specified ID
 *
 */
function domain($id) {

	$domain = Domain::get_domain($id);
	
	if($domain->getID() == "")
			return new soap_fault('Server', '', 'There is no domain with that ID.');
	else {
	
		$tempArray = array('id' => $domain->getID(),
						'domain'=>$domain->domain,
						'local'=>$domain->local
					);
	
		return $tempArray;
		
	}
}


/**
 * update_domain()
 *
 * Updates domain name and local flag and returns updated domain
 *
 */
function update_domain($id, $domain, $local) {

	$domain = Domain::update_domain($id, $domain, $local);
	
	return $domain;

}


/**
 * new_domain()
 *
 * Creates a new domain with specified name and local flag and returns it
 *
 */
function new_domain($domain, $local) {

	$domain = Domain::new_domain($domain, $local);
	
	return $domain;

}


/**
 * remove_domain()
 *
 * Destroys specified domain
 *
 */
function remove_domain($id) {

	Domain::remove_domain($id);

}


/**
 * messages()
 *
 * Returns all available messages for the current user
 *
 */
function messages($domain, $endpoint, $status, $current_user="") {

	$messages = Message::messages($domain, $endpoint, $status, $current_user);
	
	$results = array();
	
	foreach($messages as $message) {
		$tempArray = array('id' => $message->getID(),
					'uuid' => $message->getUUID(),
					'to_domain'=>$message->to_domain,
					'from_domain'=>$message->from_domain,
					'to_endpoint'=>$message->to_endpoint,
					'from_endpoint'=>$message->from_endpoint,
					'raw_message'=>$message->raw_message
				);
		array_push($results, $tempArray);

	}
	
	return $results;

}
